package mya.mes.mdc.internal.ui;

import mya.common.jaxrs.result.AddResult;
import mya.common.jaxrs.result.ListResult;
import mya.common.jpa.context.AbstractLogic;
import mya.common.jpa.context.AbstractQuery;
import mya.common.jpa.context.ServiceContext;
import mya.common.jpa.pagination.DataPage;
import mya.common.jpa.pagination.PageRange;
import mya.mes.mdc.local.mongodb.FilterDto;
import mya.mes.mdc.local.mongodb.MongoDbService;
import mya.mes.mdc.local.plc.PlcTagDefService;
import mya.mes.mdc.logic.plc.PlcTagDefServiceImpl;
import mya.mes.mdc.model.alarm.MdcAlarmType;
import mya.mes.mdc.mongodb.MongoDbServiceImpl;
import org.bson.Document;

import javax.ws.rs.*;
import java.util.ArrayList;
import java.util.List;

@Path("/{tenantId}/{loginId}/mdc/ui/")
public class MdcUi {


    @PathParam("tenantId")
    private String tenantId;

    @PathParam("loginId")
    private Long loginId;

    @GET
    @Path("test/")
    public String get() {
        return "UI ok";
    }

    @GET
    @Path("tag/")
    @Produces("application/json")
    public ListResult queryProjectFile() {

        AbstractQuery query = new AbstractQuery(tenantId, loginId, true) {
            @Override
            protected DataPage process(ServiceContext serviceContext) throws Exception {

                PlcTagDefService alarmTypeService = new PlcTagDefServiceImpl();

                alarmTypeService.updateTags(serviceContext);

                return DataPage.emptyData(MdcAlarmType.class, PageRange.NOT_PAGE);
            }
        };

        return ListResult.result(query);
    }

    @POST
    @Path("queryTagValue")
    @Produces("application/json")
    public AddResult queryTagValue(List<FilterDto> dtos) {

        AbstractLogic logic = new AbstractLogic(tenantId, loginId) {
            @Override
            protected Object process(ServiceContext serviceContext) throws Exception {

                List<Document> dataList = new ArrayList<>();

                MongoDbService service = new MongoDbServiceImpl();

                dataList = service.queryMdcData(dtos, null);

                return dataList;
            }
        };

        return AddResult.result(logic);
    }
}
